
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>樊慧玲的计算器</title>
</head>
<body>
    <input type="text" id="display" readonly>
    <br>
    <button onclick="clearDisplay()">C</button>
    <button onclick="appendNumber(7)">7</button>
    <button onclick="appendNumber(8)">8</button>
    <button onclick="appendNumber(9)">9</button>
    <button onclick="appendOperator('+')">+</button>
    <br>
    <button onclick="appendNumber(4)">4</button>
    <button onclick="appendNumber(5)">5</button>
    <button onclick="appendNumber(6)">6</button>
    <button onclick="appendOperator('-')">-</button>
    <br>
    <button onclick="appendNumber(1)">1</button>
    <button onclick="appendNumber(2)">2</button>
    <button onclick="appendNumber(3)">3</button>
    <button onclick="appendOperator('*')">*</button>
    <br>
    <button onclick="appendNumber(0)">0</button>
    <button onclick="calculateResult()">=</button>
    <button onclick="appendOperator('/')">/</button>    
</body>
<script>
    const display = document.getElementById('display');
    let currentInput = '';
    let operator = null;

    function appendNumber(number) {
        currentInput += number;
        display.value = currentInput;
    }

    function appendOperator(op) {
        if (operator !== null) {
            calculateResult();
        }
        operator = op;
        currentInput += op;
        display.value = currentInput;
    }

    function clearDisplay() {
        currentInput = '';
        operator = null;
        display.value = '';
    }

    function calculateResult() {
        if (operator === null) {
            return;
        }
        const operands = currentInput.split(operator);
        const leftOperand = parseFloat(operands[0]);
        const rightOperand = parseFloat(operands[1]);
        let result;

        switch (operator) {
            case '+':
                result = leftOperand + rightOperand;
                break;
            case '-':
                result = leftOperand - rightOperand;
                break;
            case '*':
                result = leftOperand * rightOperand;
                break;
            case '/':
                result = leftOperand / rightOperand;
                break;
        }

        currentInput = result.toString();
        operator = null;
        display.value = currentInput;
    }
</script>
</html>